/*
 * Created by JFormDesigner on Tue Aug 23 15:35:53 MSD 2011
 */

package view.swinginterface;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * @author Innokenty Shuvalov
 */
public class LoginForm extends JDialog {
    private SwingInterface owner;

    public LoginForm(SwingInterface owner) {
        super(owner);
        initComponents();
        this.owner = owner;
    }

    private boolean checkFields() {
        boolean loginCheck = checkTextField(loginField);
        boolean passwordCheck = checkTextField(passwordField);
        return loginCheck && passwordCheck;
    }

    protected static boolean checkTextField(JTextField textField) {
        if (textField.getText().equals("")) {
            textField.setBackground(Color.RED);
            return false;
        }
        textField.setBackground(Color.GREEN);
        return true;
    }

    private void loginButtonActionPerformed(ActionEvent e) {
        if (checkFields()) {
            owner.getRequestWorker().login(loginField.getText(), passwordField.getText());
            this.setVisible(false);
        }
    }

    private void registerButtonActionPerformed(ActionEvent e) {
        if (checkFields()) {
            owner.getRequestWorker().createNewUser(loginField.getText(), passwordField.getText());
            this.setVisible(false);
        }
    }

    private void inputFieldKeyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_ENTER:
                JTextField textField = (JTextField) e.getComponent();
                if (checkTextField(textField)) {
                    textField.transferFocus();
                    if (textField == passwordField) {
                        loginButtonActionPerformed(null);
                    }
                }
                break;
        }
    }

    private void inputFieldFocusGained(FocusEvent e) {
        ((JTextField) e.getComponent()).setBackground(Color.WHITE);
    }

    private void inputFieldFocusLost(FocusEvent e) {
        JTextField textField = (JTextField) e.getComponent();
        String text = textField.getText();
        if (text.equals("")) {
            textField.setBackground(Color.RED);
        } else {
            textField.setBackground(Color.GREEN);
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - iuygi iugiu
        this2 = new JPanel();
        label1 = new JLabel();
        loginField = new JTextField();
        passwordField = new JTextField();
        label2 = new JLabel();
        panel1 = new JPanel();
        registerButton = new JButton();
        loginButton = new JButton();

        //======== this ========
        setResizable(false);
        setTitle("Please Login");
        Container contentPane = getContentPane();

        //======== this2 ========
        {
            this2.setPreferredSize(new Dimension(329, 456));

            //---- label1 ----
            label1.setText("login");

            //---- loginField ----
            loginField.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    inputFieldKeyPressed(e);
                }
            });
            loginField.addFocusListener(new FocusAdapter() {
                @Override
                public void focusGained(FocusEvent e) {
                    inputFieldFocusGained(e);
                }

                @Override
                public void focusLost(FocusEvent e) {
                    inputFieldFocusLost(e);
                }
            });

            //---- passwordField ----
            passwordField.addFocusListener(new FocusAdapter() {
                @Override
                public void focusGained(FocusEvent e) {
                    inputFieldFocusGained(e);
                }

                @Override
                public void focusLost(FocusEvent e) {
                    inputFieldFocusLost(e);
                }
            });
            passwordField.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    inputFieldKeyPressed(e);
                }
            });

            //---- label2 ----
            label2.setText("password");

            //======== panel1 ========
            {
                panel1.setPreferredSize(new Dimension(194, 50));
                panel1.setLayout(new FlowLayout());

                //---- registerButton ----
                registerButton.setText("Register");
                registerButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        registerButtonActionPerformed(e);
                    }
                });
                panel1.add(registerButton);

                //---- loginButton ----
                loginButton.setText("Login");
                loginButton.setPreferredSize(new Dimension(71, 25));
                loginButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        loginButtonActionPerformed(e);
                    }
                });
                panel1.add(loginButton);
            }

            GroupLayout this2Layout = new GroupLayout(this2);
            this2.setLayout(this2Layout);
            this2Layout.setHorizontalGroup(
                    this2Layout.createParallelGroup()
                            .addGroup(this2Layout.createSequentialGroup()
                                    .addContainerGap()
                                    .addGroup(this2Layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                            .addComponent(label1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(label2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(this2Layout.createParallelGroup()
                                            .addComponent(passwordField, GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                                            .addComponent(loginField, GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE))
                                    .addContainerGap())
                            .addComponent(panel1, GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
            );
            this2Layout.setVerticalGroup(
                    this2Layout.createParallelGroup()
                            .addGroup(this2Layout.createSequentialGroup()
                                    .addContainerGap()
                                    .addGroup(this2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                            .addComponent(label1)
                                            .addComponent(loginField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                    .addGroup(this2Layout.createParallelGroup()
                                            .addGroup(this2Layout.createSequentialGroup()
                                                    .addGap(11, 11, 11)
                                                    .addComponent(label2))
                                            .addGroup(this2Layout.createSequentialGroup()
                                                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                    .addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(panel1, GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE))
            );
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addComponent(this2, GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addComponent(this2, GroupLayout.PREFERRED_SIZE, 99, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        setSize(220, 135);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - iuygi iugiu
    private JPanel this2;
    private JLabel label1;
    private JTextField loginField;
    private JTextField passwordField;
    private JLabel label2;
    private JPanel panel1;
    private JButton registerButton;
    private JButton loginButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
